#!/usr/bin/env bash

set -e
me=`basename "$0"`
mydir=`dirname "$0"`
# Either:
#  abs_srcdir and abs_builddir are set: we're running in a dev tree
#  or pkgdatadir is set: we've been installed, we respect that.
abs_srcdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."
abs_builddir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
pkgdatadir='@pkgdatadir@'
configdir='@configdir@'
# Either we've been installed and pkgdatadir exists, or we haven't been
# installed and abs_srcdir / abs_builddir aren't empty.
test -d "$pkgdatadir" || test -n "$abs_srcdir$abs_builddir" || {
  echo >&2 "$me: Uh-oh, \`$pkgdatadir' doesn't exist, is OpenTSDB properly installed?"
  exit 1
}

if test -n "$pkgdatadir"; then
  localdir="$pkgdatadir/bin"
  for jar in "$pkgdatadir"/*.jar; do
    CLASSPATH="$CLASSPATH:$jar"
  done
  # Add pkgdatadir itself so we can find logback.xml
  CLASSPATH="$CLASSPATH:$pkgdatadir"
  
  if test -d "$pkgdatadir/bin"; then
    CLASSPATH="$CLASSPATH:$pkgdatadir/bin"
  fi
  
  if test -d "$pkgdatadir/lib"; then
    for jar in "$pkgdatadir"/lib/*.jar; do
      CLASSPATH="$CLASSPATH:$jar"
    done
  fi
  
  if test -n "$configdir" && test -d "$configdir"; then
    CLASSPATH="$CLASSPATH:$configdir"
  fi
else
  localdir="$abs_builddir"
  # If we're running out of the build tree, it's especially important that we
  # know exactly what jars we need to build the CLASSPATH.  Otherwise people
  # cannot easily pick up new dependencies as we might mix multiple versions
  # of the same dependencies on the CLASSPATH, which is bad.  Looking for a
  # specific version of each jar prevents this problem.
  # TODO(tsuna): Once we jarjar all the dependencies together, this will no
  # longer be an issue.  See issue #23.
  for jar in `make -C "$abs_builddir" printdeps | sed '/third_party.*jar/!d'`; do
    for dir in "$abs_builddir" "$abs_srcdir"; do
      test -f "$dir/$jar" && CLASSPATH="$CLASSPATH:$dir/$jar" && continue 2
    done
    echo >&2 "$me: error: Couldn't find \`$jar' either under \`$abs_builddir' or \`$abs_srcdir'."
    exit 2
  done
  # Add the src dir so we can find logback.xml
  CLASSPATH="$CLASSPATH:$abs_srcdir/src"
fi
# Remove any leading colon.
CLASSPATH="${CLASSPATH#:}"

usage() {
  echo >&2 "usage: $me <command> [args]"
  echo 'Valid commands: fsck, import, mkmetric, query, tsd, scan, search, uid, version'
  exit 1
}

case $1 in
  (fsck)
    MAINCLASS=Fsck
    ;;
  (import)
    MAINCLASS=TextImporter
    ;;
  (mkmetric)
    shift
    set uid assign metrics "$@"
    MAINCLASS=UidManager
    ;;
  (query)
    MAINCLASS=CliQuery
    ;;
  (tsd)
    MAINCLASS=TSDMain
    ;;
  (scan)
    MAINCLASS=DumpSeries
    ;;
  (search)
    MAINCLASS=Search
    ;;
  (uid)
    MAINCLASS=UidManager
    ;;
  (version)
    MAINCLASS=BuildData
    ;;
  (*)
    echo >&2 "$me: error: unknown command '$1'"
    usage
    ;;
esac
shift

JAVA=${JAVA-'java'}
JVMARGS=${JVMARGS-'-enableassertions -enablesystemassertions'}
test -r "$localdir/tsdb.local" && . "$localdir/tsdb.local"

if [[ $CLASSPATH == *"asyncbigtable"* ]]
then
  USE_BIGTABLE=1
  echo "Running OpenTSDB with Bigtable support"

  exec $JAVA $JVMARGS -classpath "$CLASSPATH:$HBASE_CONF" net.opentsdb.tools.$MAINCLASS "$@"
else
  exec $JAVA $JVMARGS -classpath "$CLASSPATH" net.opentsdb.tools.$MAINCLASS "$@"
fi
